package guda.house.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.house.biz.SmsBiz;
import guda.house.dal.dao.SmsDAO;
import guda.house.dal.dataobject.SmsDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class SmsBizImpl implements SmsBiz{

    private final static Logger logger = LoggerFactory.getLogger(SmsBizImpl.class);

    @Autowired
    private SmsDAO smsDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            SmsDO smsById = smsDAO.getSmsById(id);

            bizResult.data.put("smsDO", smsById);

            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Sms error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = smsDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<SmsDO> smsList = smsDAO.selectForPage(baseQuery);
            bizResult.data.put("smsList", smsList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Sms list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            smsDAO.delSmsById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete sms error", e);
        }
        return bizResult;
    }

    public BizResult create(SmsDO smsDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = smsDAO.insert(smsDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Sms error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(SmsDO smsDO) {
        BizResult bizResult = new BizResult();
        try {
            //过滤中文逗号
            if(smsDO.getPhoneArray()!=null){
                smsDO.setPhoneArray(smsDO.getPhoneArray().replaceAll("，",","));
            }

            smsDAO.updateByIdSelective(smsDO);

            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Sms error", e);
        }
        return bizResult;
    }


}
